﻿using Framework.Cache;
using Framework.Utility.Config;
using Framework.Utility.Extensions;
using Framework.Utility.Mail;
using Support.Models;
using Support.Models.Entities;

namespace MonitorCode.Hangfire
{
    public class ServiceConfig
    {
        public async Task SendJobResultEmail(string jobId, string title, string massage)
        {
            string cacheKey = string.Format(CacheKay.JobMonitorReceiveEmail, jobId);
            var config = await CacheFactory.Cache.GetAsync<ReceiveEmailConfig>(cacheKey);
            if (string.IsNullOrWhiteSpace(config.ReceiveEmail)) return;
            var emailArr = config.ReceiveEmail.Split(";");
            foreach (var email in emailArr)
            {
                if (email.IsEamil())
                {
                    var smtp = new SMTP(email);
                    var ccList = new string[] { "lee.li@mkcorp.com" };
                    var _title = $"监控异常邮件通知({title})";
                    var _content = $"<span>您好:{email},</span><br /><p>{DateTime.Now.ToString("yyyy-MM-dd")}<br /> <br /> { title } 出现异常。<br />{massage}<br />请及时查看。</p><br /> 邮件来自{GlobalConfig.EnvironmentName} 环境。";

                    smtp.Sendemail(_title, _content, ccList);
                }
            }
        }
    }
}